package cn.dev;

import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * @author 10219990
 * @date 2023/05/24 08:46
 **/
public class DefaultModuloPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Long> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Long> preciseShardingValue) {
        String modulo = String.valueOf(preciseShardingValue.getValue() % collection.size());

        for (String targetName : collection) {
            if (targetName.endsWith(modulo)) {
                return targetName;
            }
        }
        throw new UnsupportedOperationException();
    }
}
